package com.dh.project.controller;

import com.dh.project.model.EmployeeInfo;
import com.dh.project.model.OrderInfo;
import com.dh.project.model.PageInfo;
import com.dh.project.model.PageModel;
import com.dh.project.other.ModelRes;
import com.dh.project.service.EmployeeInfoService;
import com.dh.project.service.OrderInfoService;
import com.dh.project.utils.PageUtil;
import com.dh.project.view.EmployeeView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;


/** 工人Controller
 * Created by admin on 2017/4/5.
 */
@Controller
public class EmployeeController {

    private EmployeeInfoService employeeInfoService;

    private OrderInfoService orderInfoService;

    @Autowired
    public EmployeeController(EmployeeInfoService employeeInfoService,OrderInfoService orderInfoService){
        this.employeeInfoService = employeeInfoService;
        this.orderInfoService = orderInfoService;

    }

    /**
     * 工人信息列表
     * @param model
     * @return
     */
    @RequestMapping(value = "selectEmployeeList",produces = "text/html;charset=utf-8")
    public String selectEmployeeList(EmployeeInfo employee ,int page,Model model){
        Map map = new HashMap();
        map.put("employee_id",employee.getEmployee_id());
        map.put("employee_name",employee.getEmployee_name());

        map.put("page_start",(page-1)*10);
        map.put("page_end",10);

        List<EmployeeInfo> employeeInfoList = employeeInfoService.selectEmployeeInfoListByMap(map);

        List<EmployeeView> employeeViews = new ArrayList<>();
        for (int i = 0; i < employeeInfoList.size(); i++) {
            Map mapOrder = new HashMap();
            mapOrder.put("order_status",7);
            mapOrder.put("employee_id",employeeInfoList.get(i).getEmployee_id());
            List<OrderInfo> orderInfos = orderInfoService.selectByMap(mapOrder);
            EmployeeView employeeView = new EmployeeView();
            employeeView.setCount_order(orderInfos.size());
            employeeView.setEmployeeInfo(employeeInfoList.get(i));
            employeeViews.add(employeeView);
        }

        PageModel pageModel = employeeInfoService.selectEmployeeInfoListByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHref(employee,3));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));

        model.addAttribute("pageInfo",pageInfo);

        model.addAttribute("employeeList",employeeViews);
        model.addAttribute("employee",employee);
        return "employee_list";
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertEmployee",produces = "text/html;charset=utf-8")
    public Object insertEmployee(EmployeeInfo employee){
        if (employee == null){
            return new ModelRes(ModelRes.Status.ERROR,"添加失败!");
        }
        employee.setEmployee_status(1+"");
        employee.setGrade_number(5);
        employee.setIs_freeze(1);
        employee.setMath_code("123456");
        employee.setLogin_code("无");
        employee.setCreatetime(String.valueOf(new Date().getTime()));
        employee.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = employeeInfoService.insert(employee);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败!");
        }
    }

    /**
     * 修改工人信息
     * @param employee
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateEmployee",produces = "text/html;charset=utf-8")
    public Object updateEmployee(EmployeeInfo employee){
        if (employee.getEmployee_phone() != null){
            Map map = new HashMap();
            map.put("employee_phone",employee.getEmployee_phone());
            List<EmployeeInfo> employeeInfos = employeeInfoService.selectByMap(map);
            if (employeeInfos.size() != 0){
                if (employeeInfos.get(0).getEmployee_id() != employee.getEmployee_id()){
                    return new ModelRes(ModelRes.Status.ERROR,"此电话号码重复，修改失败!");
                }
            }
        }
        EmployeeInfo employeeInfo = employeeInfoService.selectById(employee.getEmployee_id());
        employeeInfo.setEmployee_phone(employee.getEmployee_phone());
        employeeInfo.setEmployee_name(employee.getEmployee_name());
        employeeInfo.setEmployee_type_one(employee.getEmployee_type_one());
        employeeInfo.setEmployee_type_two(employee.getEmployee_type_two());
        employeeInfo.setEmployee_type_three(employee.getEmployee_type_three());
        employeeInfo.setEmployee_type_four(employee.getEmployee_type_four());
        employeeInfo.setEmployee_type_five(employee.getEmployee_type_five());
        employeeInfo.setEmployee_type_six(employee.getEmployee_type_six());
        employeeInfo.setManage_address(employee.getManage_address());
        employeeInfo.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = employeeInfoService.updateById(employeeInfo);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败!");
        }
    }

    /**
     * 删除工人
     * @param admin_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteEmployee",produces = "text/html;charset=utf-8")
    public Object deleteEmployee(int admin_id){
        boolean isSuccess = employeeInfoService.deleteById(admin_id);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"删除失败!");
        }
    }

    /**
     * 冻结工人账户
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "dongJieEmployee",produces = "text/html;charset=utf-8")
    public Object dongJieEmployee(int id){
        EmployeeInfo employeeInfo = employeeInfoService.selectById(id);
        Map map = new HashMap();
        map.put("employee_id",id);
        List<OrderInfo> orderInfos = orderInfoService.selectByMap(map);
        //判断该工人有没有正在进行中的订单
        if (orderInfos.size() > 0){
            for (int i = 0; i < orderInfos.size(); i++) {
                if (orderInfos.get(0).getOrder_status() != 7){
                    return new ModelRes(ModelRes.Status.ERROR,"该工人有正在进行的订单，无法冻结!");
                }
            }
        }
        if (employeeInfo.getIs_freeze() == 1){
            employeeInfo.setIs_freeze(2);
        }else if (employeeInfo.getIs_freeze() == 2){
            employeeInfo.setIs_freeze(1);
        }
        employeeInfo.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = employeeInfoService.updateById(employeeInfo);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"操作失败!");
        }
    }
}
